package com.staff.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.common.util.AJAXResultMessage;
import com.institute.dao.InstituteRepository;
import com.institute.dao.PositionRepository;
import com.institute.entity.Institute;
import com.institute.entity.Position;
import com.staff.dao.StaffArchiveRepository;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;
import com.staff.entity.StaffArchive;
import com.staff.entity.dto.StaffQueryDTO;
import com.transfer.dao.PersonTransferRepository;
import com.transfer.dao.TitleAppointRepository;
import com.transfer.entity.PersonTransfer;
import com.transfer.entity.TitleAppoint;

import net.sf.ehcache.util.FindBugsSuppressWarnings;

@Service
@Transactional
public class StaffService implements IStaffService {
	
	@Autowired
	private StaffRepository staffRepository;
	
	@Autowired
	private TitleAppointRepository titleAppointRepository;
	
	@Autowired
	private PersonTransferRepository personTransferRepository;
	
	@Autowired
	private InstituteRepository instituteDao;
	
	@Autowired
	private PositionRepository positionDao;

	public AJAXResultMessage saveOrUpdate(StaffQueryDTO staffDTO) {
		//如果是新建员工数据则进入新建档案if
		
		if(staffDTO.getId()==null) {
			if(null!=staffRepository.findOne(staffDTO.getStaffNum()))
				return new AJAXResultMessage(false,"员工编号已存在!");
			Staff staff = new Staff();
			Institute department = instituteDao.findByDeptName(staffDTO.getDepartment());
			Position position = positionDao.findByName(staffDTO.getPosition());
			BeanUtils.copyProperties(staffDTO,staff);
			staff.setDepartment(department);
			staff.setPosition(position);
			staffRepository.save(staff);
			return new AJAXResultMessage(true,"操作成功！");
		}
		//如果是修改员工数据则进入添加人事调动记录if、职称聘任记录if
		if(staffDTO.getId()!=null) {
			Staff staff = staffRepository.findOne(staffDTO.getId());
			Date date = new Date();
			//判断职位部门是否变动
			if(!staff.getDepartment().getName().equals(staffDTO.getDepartment())||!staff.getPosition().equals(staffDTO.getPosition())) 
			{
				
				Institute oldDept = staff.getDepartment();
				//如果是原部门负责人，去掉负责人
				if(oldDept!=null&&oldDept.getPrincipal()!=null) {
					if(oldDept.getPrincipal().getId() == oldDept.getId()) {
						oldDept.setPrincipal(null);
						instituteDao.save(oldDept);
					}
				}
				
				PersonTransfer pt = new PersonTransfer();
				//添加人事调动记录
				pt.setStaffNum(staff.getStaffNum());
				pt.setStaffName(staff.getStaffName());
				pt.setOriginalDepa(staff.getDepartment().getName());
				pt.setOriginalPosi(staff.getPosition().getName());
				pt.setNewDepa(staffDTO.getDepartment());
				pt.setNewPosi(staffDTO.getPosition());
				pt.setDeployDate(date);
				
				Institute department = instituteDao.findByDeptName(staffDTO.getDepartment());
				Position position = positionDao.findByName(staffDTO.getPosition());
				staff.setDepartment(department);
				staff.setPosition(position);
				
				pt.setStaff(staff);
				personTransferRepository.save(pt);
			}
			//判断职称是否变动
			if(!staff.getTitle().equals(staffDTO.getTitle())) {
				Staff newStaff = staffRepository.findOne(staffDTO.getId());
				TitleAppoint ta = new TitleAppoint();
				//添加职称聘任记录
				ta.setStaffNum(newStaff.getStaffNum());
				ta.setStaffName(newStaff.getStaffName());
				ta.setAppointDate(date);
				ta.setAppointTitle(staffDTO.getTitle());			
				ta.setStaff(newStaff);
				
				titleAppointRepository.save(ta);
			}
			return new AJAXResultMessage(true,"操作成功!");
		}
		return new AJAXResultMessage(false,"员工不存在！");
	}
	public void delete(Staff staff) {
		staffRepository.delete(staff);

	}
	public Staff findOne(Long id) {
		return staffRepository.findOne(id);
	}
	
	public Staff findOne(String staffNum) {
		
		return staffRepository.findOne(staffNum);
	}
	
	public List<Staff> findAll() {
		return (List<Staff>) staffRepository.findAll();
	}


	public void delete(List<Long> ids) {
		List<Staff> users = (List<Staff>)staffRepository.findAll(ids);
		if(users!=null) {
			staffRepository.delete(users);
		}
	}


	public Page<StaffQueryDTO> findAll(Specification<Staff> spec, Pageable pageable) {
		Page<Staff> page = staffRepository.findAll(spec, pageable);
		List<StaffQueryDTO> results = new ArrayList<StaffQueryDTO>();
		for(Staff staff:page.getContent()) {
			StaffQueryDTO dto = new StaffQueryDTO();
			BeanUtils.copyProperties(staff,dto);
			dto.setDepartment(staff.getDepartment().getName());
			dto.setPosition(staff.getPosition().getName());
			results.add(dto);
		}
		return new PageImpl<>(results, pageable, page.getTotalElements());
	}

	//查找员工档案
	public StaffArchive findStaffArchive(Staff staff) {
		Staff s = findOne(staff.getId());
		return staffRepository.findStaffArchive(staff.getId());
	}
	@Override
	public Staff findStaff(String staffNum, String password) {
		return staffRepository.findStaff(staffNum, password);
	}

}
